$(document).ready(function () {

    InitBinding();
});



function InitBinding() {

    $("#IncludeRating").click(function (e) {


        if ($('#IncludeRating').is(':checked')) {
            $('#Rating').removeAttr('disabled');
        }
        else {
            $('#Rating').attr('disabled', '');
        }

    });
    

    //	On submit on add page, submit the add and shows the success
    //	page it the edit was successful, otherwise add page is shown again including
    //	validation errors
    $("#SearchSubmit").click(function (e) {
        e.preventDefault();

        $("#CreateSearchForm").submit();
        $('#allFormData').hide();
        $('#loader').show();
    });


    if ($('successfulCreate').length > 0) {
        var successfulCreate = $('#successfulCreate').val();
        if (successfulCreate == "False") {
            showOkDialog('Your search is invalid', 180, 'Error');
        }
    }

}




//Telerik grid setup
function SearchResultsGrid_onRowDataBound(e) {
    var dataItem = e.dataItem;
    var snippetId = dataItem.CodeSnippetId;

    $(e.row).find("a.popup-button")
                    .click(function (e) {
                        ShowSnippetPopup(snippetId);
                    });

    $(e.row).find("a.show-button")
                .click(function (e) {
                    window.location.href = '/CodeSnippet/DisplaySnippetsForAddAndEdit' + '?codeSnippetId=' + snippetId + "&wasAddOrEdit=false";
                });
}


function ShowSnippetPopup(snippetId) {


    $.post("/Search/GetSpecificSnippetData", { snippetId: snippetId },
            function (data) {
                if (data.Success != undefined && data.Success) {

                    var codeSnippetContents = '<pre id="preCode_' + snippetId + '" class="' + data.PreClass + ' searchsnippetPopup">' + data.CodeSnippetCode + '</pre>'
                    showDialogWidthAndHeightNoCallbackAndNoScroll(codeSnippetContents,
                        600, 450, 'Displaying Snippet : ' + snippetId);
                    SwitchHightlighting(data.HighlightingCSSName);
                }
                else {
                    showOkDialog(data.Message, 180, 'Error');
                }
            },
            "json");

}